<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/21/0021
 * Time: 17:32
 */

namespace app\sports_admin\model;


use think\Model;
use think\Request;
use app\exception\SportProjectException;

class SportProject extends Model
{
    protected $autoWriteTimestamp = true;

    protected $table = 'sport_project';

    protected $pk = 'id';

    /**
     * @param $page 页数
     * @param $rows 每页行数
     * @param $sort 排序字段
     * @param $sortOrder 排序方式
     * 项目分类
     */
    public static function sportProjects($page,$rows,$sort,$sortOrder)
    {
        $adminList = self::page($page)
            ->limit($rows)
            ->order("{$sort}","{$sortOrder}")->select();

        $adminCount = self::count();
        return ['rows'=>$adminList,'total'=>$adminCount];
    }
    /**
     * 添加项目分类
     */
    public static function sportProjectAdd($title)
    {
        $is_add = self::create(['title'=>$title]);
        if(!$is_add){
            throw new SportProjectException(['errorCode'=>400,'msg'=>"添加失败"]);
        } return json(['code'=>200,'msg'=>"添加成功"]);
    }
    /**
     * 判断是否重名
     */
    public static function sportProjectRepeat($title)
    {
        return self::where('title',$title)->find();
    }
    /**
     * 删除项目分类
     */
    public static function sportProjectDel()
    {
        $data = Request::instance()->only('id');
        return self::where('id',$data['id'])->delete();
    }
}